package com.yw.thesimpllestplayer.mediaplayer.decoder;

import android.media.MediaCodec;
import android.media.MediaFormat;
import android.util.Log;
import android.view.Surface;

import com.yw.thesimpllestplayer.extractor.IExtractor;
import com.yw.thesimpllestplayer.extractor.VideoExtractor;

import java.nio.ByteBuffer;

/**
 * @ProjectName: TheSimpllestplayer
 * @Package: com.yw.thesimpllestplayer.mediaplayer.decoder
 * @ClassName: VideoDecoder
 * @Description: 视频解码器
 * @Author: wei.yang
 * @CreateDate: 2021/11/6 13:49
 * @UpdateUser: 更新者：wei.yang
 * @UpdateDate: 2021/11/6 13:49
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class VideoDecoder extends BaseDecoder {
    private static final String TAG = "VideoDecoder";
    private Surface surface;

    public VideoDecoder(String filePath, Surface surface) {
        super(filePath);
        this.surface = surface;
    }

    @Override
    public boolean check() {
        if (surface == null) {
            Log.e(TAG, "Surface不能为空");
            if (mStateListener != null) {
                mStateListener.decoderError(this, "显示器为空");
            }
            return false;
        }
        return true;
    }

    @Override
    public IExtractor initExtractor(String filePath) {
        return new VideoExtractor(filePath);
    }

    @Override
    public void initSpecParams(MediaFormat format) {

    }

    @Override
    public boolean configCodec(MediaCodec codec, MediaFormat format) {
        if (surface != null) {
            codec.configure(format, surface, null, 0);
            notifyDecode();
        } else {
            if (mStateListener != null) {
                mStateListener.decoderError(this, "配置解码器失败，因为Surface为空");
            }
            Log.e(TAG, "配置解码器失败，因为Surface为空");
            return false;
        }
        return true;
    }

    @Override
    public boolean initRender() {
        return true;
    }

    @Override
    public void render(ByteBuffer outputBuffer, MediaCodec.BufferInfo bufferInfo) {

    }

    @Override
    public void finishDecode() {

    }
}
